package drawingBoard.myFile;

import drawingBoard.ImageFrame;
import drawingBoard.ImageService;
import drawingBoard.MyImage;
import drawingBoard.tool.AbstractTool;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;

/**
 *  类 MyImageFile 关联 ImageFileChooser
 *
 */
public class MyImageFile {
    private ImageFileChooser fileChooser = new ImageFileChooser();

    public void save(boolean b, ImageFrame frame) {  //保存图片
        if(b){
            // 如果选择保存
            if (fileChooser.showSaveDialog(frame)==ImageFileChooser.APPROVE_OPTION) {
                //获取当前路径
                File currentDirectory=fileChooser.getCurrentDirectory();
                //获取文件名
                String fileName=fileChooser.getSelectedFile().getName();
                //获取后缀名
                String suf=fileChooser.getSuf();
                //组合保存路径
                String savePath=currentDirectory+"\\"+fileName+"."+suf;
                try {
                    //将图片写到保存路径
                    ImageIO.write(frame.getBufferedImage(), suf, new File(savePath));
                } catch (java.io.IOException ie) {
                    ie.printStackTrace();
                }
                frame.setTitle(fileName+"."+suf+" - 画图");  //设置保存后的窗口标题
                frame.getBufferedImage().setIsSaved(true);   //已保存
            }
        } else if (!frame.getBufferedImage().isSaved()) {
            //新建一个对话框
            //显示确认保存对话框YES_NO_OPTION
            int checked = JOptionPane.showConfirmDialog(frame, "保存改动?", "画图",
                    JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE);
            if (checked == JOptionPane.YES_OPTION) {  //如果选择是
                save(true,frame);  //保存图片
            }
        }
    }

    public void open(ImageFrame frame) {  //打开图片
        save(false, frame);
        //如果打开一个文件
        if (fileChooser.showOpenDialog(frame)==ImageFileChooser.APPROVE_OPTION) {
            // 获取选择的文件
            File file = fileChooser.getSelectedFile();
            // 设置当前文件夹
            fileChooser.setCurrentDirectory(file);
            BufferedImage image = null;
            try {
                image = ImageIO.read(file);  //从文件读取图片
            } catch (java.io.IOException e) {
                e.printStackTrace();
                return;
            }
            int width = image.getWidth();
            int height = image.getHeight();
            AbstractTool.drawWidth = width;
            AbstractTool.drawHeight = height;
            // 创建一个MyImage
            MyImage myImage = new MyImage(width,height,BufferedImage.TYPE_INT_RGB);
            // 把读取到的图片画到myImage上面
            myImage.getGraphics().drawImage(image,0,0,width, height,null);
            frame.setBufferedImage(myImage);
            frame.getDrawSpace().repaint();  //repaint
            //重新设置viewport
            ImageService.setViewport(frame.getScroll(),frame.getDrawSpace(),width,height);
            //设置保存后的窗口标题
            frame.setTitle(fileChooser.getSelectedFile().getName() + " - 画图");
        }
    }

    public void createGraphics(ImageFrame frame) {  //新图片
        save(false, frame);
        int width=(int)getScreenSize().getWidth()/2;
        int height=(int)getScreenSize().getHeight()/2;
        AbstractTool.drawWidth=width;
        AbstractTool.drawHeight=height;
        // 创建一个MyImage
        MyImage myImage = new MyImage(width, height, BufferedImage.TYPE_INT_RGB);
        Graphics g = myImage.getGraphics();
        g.setColor(Color.WHITE);
        g.fillRect(0, 0, width, height);
        frame.setBufferedImage(myImage);
        // repaint
        frame.getDrawSpace().repaint();
        // 重新设置viewport
        ImageService.setViewport(frame.getScroll(),frame.getDrawSpace(),width,height);
        // 设置保存后的窗口标题
        frame.setTitle("未命名 - 画图");
    }

    public static Dimension getScreenSize() {  //获取屏幕的分辨率
        Toolkit dt = Toolkit.getDefaultToolkit();
        return dt.getScreenSize();
    }
}
